package cn.iocoder.yudao.module.xhyy.service.hearing;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.module.xhyy.controller.app.hearing.vo.AppHearingPageReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.xhyy.controller.admin.hearing.vo.*;
import cn.iocoder.yudao.module.xhyy.dal.dataobject.hearing.HearingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.xhyy.dal.mysql.hearing.HearingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.sypz.enums.ErrorCodeConstants.HEARING_NOT_EXISTS;

/**
 * 听力资源 Service 实现类
 *
 * @author duanxinli
 */
@Service
@Validated
public class HearingServiceImpl implements HearingService {

    @Resource
    private HearingMapper hearingMapper;

    @Override
    public String createHearing(HearingSaveReqVO createReqVO) {
        // 插入
        HearingDO hearing = BeanUtils.toBean(createReqVO, HearingDO.class);
        hearing.setId(IdUtil.fastSimpleUUID());
        hearingMapper.insert(hearing);
        // 返回
        return hearing.getId();
    }

    @Override
    public void updateHearing(HearingSaveReqVO updateReqVO) {
        // 校验存在
        validateHearingExists(updateReqVO.getId());
        // 更新
        HearingDO updateObj = BeanUtils.toBean(updateReqVO, HearingDO.class);
        hearingMapper.updateById(updateObj);
    }

    @Override
    public void deleteHearing(String id) {
        // 校验存在
        validateHearingExists(id);
        // 删除
        hearingMapper.deleteById(id);
    }

    private void validateHearingExists(String id) {
        if (hearingMapper.selectById(id) == null) {
            throw exception(HEARING_NOT_EXISTS);
        }
    }

    @Override
    public HearingDO getHearing(String id) {
        return hearingMapper.selectById(id);
    }

    @Override
    public PageResult<HearingDO> getHearingPage(HearingPageReqVO pageReqVO) {
        return hearingMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<HearingDO> getHearingPage(AppHearingPageReqVO pageReqVO) {
        return hearingMapper.selectPage(pageReqVO);
    }
}